package com.yun.algorithm.backtracking.leetcode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class leetcode39 {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<>();
        dfs(candidates, target, 0, new LinkedList<>(), result);
        return result;
    }

    private void dfs(int[] candidates, int target, int start,
                     LinkedList<Integer> stack,
                     List<List<Integer>> result) {
        if (target == 0) {
            result.add(new ArrayList<>(stack));
            return;
        }
        for (int i = start; i < candidates.length; i++) {
            int candidate = candidates[i];
            if (target < candidate) {
                continue;
            }
            stack.push(candidates[i]);
            dfs(candidates, target - candidate, i, stack, result);
            stack.pop();
        }
    }

    public static void main(String[] args) {
        int[] candidate = {8, 7, 4, 3};
        int target = 11;
        System.out.println(new leetcode39().combinationSum(candidate, target));
    }
}
